package com.aqarat.managedBean;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Customer;
import com.core.managedBean.BaseBean;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class CustomerSearchBean extends BaseBean {
	private static final long serialVersionUID = 1L;

	private Customer customer;

	private AqaratController aqaratController;
	LazyDataModel<Customer> lazyLoad = new LazyDataModel<Customer>() {
		@Override
		public List<Customer> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
			lazyLoad.setRowCount(aqaratController.getCustomerListRowCount(customer).intValue());
			return aqaratController.getCustomerList(first, pageSize, sortField, sortOrder, filters,customer);
		}
	};
	@PostConstruct
	public void loadData() {
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
		if(customer ==null){
			customer=new Customer();
		}
	}

	public void searchCustomer() {
		try {
			getCustomerList();
		} catch (Exception e) {
			super.addErrorMessage("An Error Occured");
			e.printStackTrace();
		}
	}

	// ---------------------------------------------------------//
	public LazyDataModel<Customer> getCustomerList() {
		return lazyLoad;
	}
	public final Customer getCustomer() {
		return customer;
	}

	public final void setCustomer(Customer customer) {
		this.customer = customer;
	}

}
